﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CNotebook;
using DevExpress.XtraBars.Ribbon;
namespace NotebookS2
{
    public partial class M_USERMAINTENANCE : RibbonForm
    {
        public M_USERMAINTENANCE()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }
        public string ActiveUserID { get; set; }

        public void BindDepartment()
        {
            DepartmentManager oManager = new DepartmentManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            cboDepartment.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("DeptName"));
            cboDepartment.Properties.DisplayMember = "DeptName";
            cboDepartment.Properties.ValueMember = "DeptName";

            cboDepartment.Properties.DataSource = oManager.GetDepartment();
            cboDepartment.Properties.ShowHeader = false;
            cboDepartment.Refresh();

            oManager.Close();
        }

        void BindWarehouse()
        {
            WarehouseManager oManager = new WarehouseManager();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable oTable = new DataTable();

            oTable = oManager.GetWarehouse();

            cboDefaultWarehouse.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Dscription"));
            cboDefaultWarehouse.Properties.DisplayMember = "Dscription";
            cboDefaultWarehouse.Properties.ValueMember = "Code";

            cboDefaultWarehouse.Properties.DataSource = oManager.GetWarehouse();
            cboDefaultWarehouse.Properties.ShowHeader = false;
            cboDefaultWarehouse.Refresh();

        }


        void BindUserPolicies()
        {
            PolicyManager oManager = new PolicyManager();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable oTable = new DataTable();

            oTable = oManager.GetPolicies();

            cboAccessCode.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("PolicyGroupName"));
            cboAccessCode.Properties.DisplayMember = "PolicyGroupName";
            cboAccessCode.Properties.ValueMember = "GroupCode";

            cboAccessCode.Properties.DataSource = oManager.GetPolicies();
            cboAccessCode.Properties.ShowHeader = false;
            cboAccessCode.Refresh();

        }


        private void txtBPCode_Properties_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {

        }

        private void M_UserMaintenance_Load(object sender, EventArgs e)
        {
            BindUserPolicies();
            BindWarehouse();
            BindDepartment();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtEmpID_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 0)
            {
                FND_EMPLOYEE oSearch = new FND_EMPLOYEE();
                EmployeeManager oManger = new EmployeeManager();
                oSearch.ConnectionString = this.ConnectionString;
                oSearch.ShowDialog();

                string sEmpID = oSearch.EmployeeID;

                if (sEmpID != null)
                {
                    txtEmpID.Text = sEmpID;
                    txtEmpName.Text = oSearch.EmployeeFullName;
                    oManger.ConnectionString = this.ConnectionString;
                    oManger.Open();
                    DataTable _Table = oManger.GetEmployeeInfo(sEmpID);
                    oManger.Close();

                    DataRow oRow = _Table.Rows[0];

                    UserManager oManager = new UserManager();

                    oManager.ConnectionString = this.ConnectionString;
                    oManager.Open();

                    DataTable dtUser = oManager.GetUserByEmployeeID(sEmpID);


                    if (dtUser.Rows.Count > 0)
                    {
                        btnAdd.Text = "Update";
                        DataRow dRowUser = dtUser.Rows[0];

                        cboDepartment.EditValue = dRowUser["Department"].ToString();
                        cboAccessCode.EditValue = dRowUser["AccessCode"].ToString();
                        cboDefaultWarehouse.EditValue = dRowUser["DefaultWhseCode"].ToString();
                        chkMultiWarehouse.Checked = Convert.ToBoolean(dRowUser["IsMultiWh"]);

                        txtUserID.EditValue = dRowUser["UserID"].ToString();
                        txtPassword.EditValue = dRowUser["UserPassword"].ToString();
                        txtConfirmPassword.EditValue = dRowUser["UserPassword"].ToString();
                    }
                    else
                    {
                       
                        cboDepartment.EditValue = null;
                        cboAccessCode.EditValue = null;
                        cboDefaultWarehouse.EditValue = null;

                        chkMultiWarehouse.Checked = false;

                        txtUserID.EditValue = null;
                        txtPassword.EditValue = null;
                        txtConfirmPassword.EditValue = null;

                        btnAdd.Text = "Add";
                    }


                    oManager.Close();


                }
            }
            else
            {
                txtEmpID.Text = "";
                txtEmpName.Text = "";
                txtPassword.Text = "";

                cboDepartment.EditValue = null;
                cboAccessCode.EditValue = null;
                cboDefaultWarehouse.EditValue = null;

                chkMultiWarehouse.Checked = false;

                txtUserID.EditValue = null;
                txtPassword.EditValue = null;
                txtConfirmPassword.EditValue = null;

                btnAdd.Text = "Add";
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            UserManager oManager = new UserManager();
            UserUnit oUnit = new UserUnit();

            if (txtConfirmPassword.Text == txtPassword.Text)
            {
                if (btnAdd.Text == "Add")
                {


                    oManager.ConnectionString = this.ConnectionString;
                    oManager.Open();

                    if (oManager.IsUserExists(txtUserID.Text) == false)
                    {

                        oUnit.UserID = txtUserID.Text;
                        oUnit.Password = txtPassword.Text;
                        oUnit.EmployeeID = txtEmpID.Text;
                        oUnit.UserName = txtEmpName.Text;
                        oUnit.Department = cboDepartment.EditValue.ToString();
                        oUnit.AccessCode = cboAccessCode.EditValue.ToString();
                        oUnit.IsMultiWarehouse = chkMultiWarehouse.Checked;
                        oUnit.DefaultWarehouse = cboDefaultWarehouse.EditValue.ToString();

                        oManager.AddUser(oUnit);
                        oManager.Close();
                        btnAdd.Text = "Update";

                        //MainMenu oForm = (MainMenu)this.MdiParent;

                        //oForm.ErrMsg.ImageIndex = 8;
                        //oForm.ErrMsg.Caption = "Adding document is successfull!!!";
                        //oForm.timer1.Enabled = true;
                        MessageBox.Show("New user is successfully saved!!!", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("User ID already in used!!! Please try another ID!!!", "Duplicate ID", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txtUserID.Focus();
                        txtUserID.SelectAll();

                    }
                }
                else
                {
                    oManager.ConnectionString = this.ConnectionString;
                    oManager.Open();

                    oUnit.UserID = txtUserID.Text;
                    oUnit.Password = txtPassword.Text;
                    oUnit.EmployeeID = txtEmpID.Text;
                    oUnit.UserName = txtEmpName.Text;
                    oUnit.Department = cboDepartment.EditValue.ToString();
                    oUnit.AccessCode = cboAccessCode.EditValue.ToString();
                    oUnit.IsMultiWarehouse = chkMultiWarehouse.Checked;
                    oUnit.DefaultWarehouse = cboDefaultWarehouse.EditValue.ToString();

                    oManager.UpdateUser(oUnit);
                    oManager.Close();

                    MessageBox.Show("New user is successfully updated!!!", "Update User Profile", MessageBoxButtons.OK, MessageBoxIcon.Information);
                   // MainMenu oForm = (MainMenu)this.MdiParent;

                   //// oForm.ErrMsg.ImageIndex = 8;
                   // oForm.ErrMsg.Caption = "Updating document is successfull!!!";
                   // oForm.timer1.Enabled = true;
                }
            }
            else
            {
                MessageBox.Show("Password doesn't match!!!", "Confirm Password", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }
    }
}
